<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="/taglibs.jsp"%>
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">  
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link href="${ctx}/resources/semantic/dist/semantic.min.css" rel="stylesheet" type="text/css" />
    <link href="${ctx}/resources/apps.css" rel="stylesheet" type="text/css" />
     
<script src="${ctx}/resources/semanticui/jquery-2.1.4.min.js"></script>
<script src="${ctx}/resources/semantic/dist/semantic.min.js" type="text/javascript"></script>
    
    <title>发布职位</title>  
</head>
<body>

<div class="ui segment" id="n-page">

        <div class="ui segment" id="n-content" style="border:none;">
            <form class="ui form">
                <div class="field">
                    <label>职位</label>
                    <div class="ui search" id="jobtitle" name="jobtitle">
                        <div class="ui input">
                            <input class="prompt" type="text" placeholder="请输入职位名称" value="${position.name}">
                        </div>
                        <div class="results"></div>
                    </div>
                </div>
                 <div class="field">
                    <label>职位类别</label>
                     <select class="ui search dropdown" name="roleid" id="roleid">
                     <option value="">请选择职位类别</option>
                     <c:forEach items="${roles}" var="role">
                        <c:choose>
                            <c:when test="${position.roleid == role.id }">
                                <option value="${role.id}" selected="selected">${role.name}</option>
                            </c:when> 
                            <c:otherwise> 
                                <option value="${role.id}">${role.name}</option>
                            </c:otherwise> 
                        </c:choose>  
                    </c:forEach>
                    </select>
                </div>
                <div class="field">
                    <label>所在城市</label>
                    <select name="cityId" id="cityId"  class="ui search dropdown">
                    <option value="">请输入所在城市名称</option>
                    <c:forEach items="${cities}" var="city">  
                        <c:choose>
                            <c:when test="${position.cityId == city.id }">
                                <option value="${city.id}" selected="selected">${city.name}</option>
                            </c:when> 
                            <c:otherwise> 
                                <option value="${city.id}">${city.name}</option>
                            </c:otherwise> 
                        </c:choose>  
                    </c:forEach>
                    </select>
                </div>
                <div class="field">
                    <label>负责区域</label>
                    <div class="ui input">
                        <input type="text" name="location" id="location" value="${position.location}" placeholder="请输入区域">
                    </div>
                </div>
                <div class="field">
                    <label>下属人数</label>
                    <div class="ui input">
                        <input type="text" name="employeeNum" id="employeeNum" value="${position.employeeNum}" placeholder="请输入下属人数">
                    </div>
                </div>
                <div class="field">
                    <label>主要职责</label>
                    <div class="ui input">
                        <input type="text" name="majorDuty" id="majorDuty" value="${position.majorDuty}" placeholder="请输入主要职责">
                    </div>
                </div>
                <div class="field">
                    <label>推荐奖金</label>
                    <div class="ui input">
                        <input type="text" name="reward" id="reward" value="${position.reward}" placeholder="请输入推荐奖金">
                    </div>
                </div>
                  <!-- div class="field">
                    <label>期望候选人职位</label>
                    <div class="ui search input">
                        <input type="text" name="expectJob" id="expectJob" value="${position.expectJob}" placeholder="请输入期望候选人职位">
                    </div>
                </div> 
	-->
                 <div class="field">
                    <label>期望候选人职位</label>
                    <select name="expectJob" id ="expectJob" class="ui search dropdown">
                    <option value="">请输入期望担任职位名称</option>
                    <c:forEach items="${roles}" var="role">  
                        <c:choose>
                            <c:when test="${position.expectJobId == role.id }">
                                <option value="${role.id}" selected="selected">${role.name}</option>
                            </c:when> 
                            <c:otherwise> 
                                <option value="${role.id}">${role.name}</option>
                            </c:otherwise> 
                        </c:choose>  
                    </c:forEach>
                    </select>
                </div> 
		
		
                <div class="field">
                        <label>期望候选人细分领域</label> 
                        <select class="ui search selection dropdown"
                            name="majordomain" id="majordomain" multiple="">
                            <option value="">请输入擅长领域</option>
                            <c:forEach items="${domains}" var="dm">

                                <option value="${dm.id}">${dm.name}</option>

                            </c:forEach>
                        </select>

                    </div>
                <div class="field">
                    <label>期望公司来源1</label>
                    <select class="ui search dropdown" name="expectcompanyid" id="expectcompanyid">
                    <option value="">请输入期望来源公司名</option>
                    <c:forEach items="${companies}" var="company">  
                        <c:choose>
                            <c:when test="${position.expectcompanyid == company.id }">
                                <option value="${company.id}" selected="selected">${company.name}</option>
                            </c:when> 
                            <c:otherwise> 
                                <option value="${company.id}">${company.name}</option>
                            </c:otherwise> 
                        </c:choose>  
                    </c:forEach>              
                    </select>
                </div>
                <div class="field">
                    <label>期望公司来源2</label>
                    <select class="ui search dropdown" name="expectcompanyid2" id="expectcompanyid2">
                    <option value="">请输入期望来源公司名</option>
                    <c:forEach items="${companies}" var="company">  
                        <c:choose>
                            <c:when test="${position.expectcompanyid2 == company.id }">
                                <option value="${company.id}" selected="selected">${company.name}</option>
                            </c:when> 
                            <c:otherwise> 
                                <option value="${company.id}">${company.name}</option>
                            </c:otherwise> 
                        </c:choose>  
                    </c:forEach>              
                    </select>
                </div>
                <div class="field">
                    <label>期望公司来源3</label>
                    <select class="ui search dropdown" name="expectcompanyid3" id="expectcompanyid3">
                    <option value="">请输入期望来源公司名</option>
                    <c:forEach items="${companies}" var="company">  
                        <c:choose>
                            <c:when test="${position.expectcompanyid3 == company.id }">
                                <option value="${company.id}" selected="selected">${company.name}</option>
                            </c:when> 
                            <c:otherwise> 
                                <option value="${company.id}">${company.name}</option>
                            </c:otherwise> 
                        </c:choose>  
                    </c:forEach>              
                    </select>
                </div>
                <div class="field">
                    <label>有效期</label>
                    <div class="ui input">
                        <input type="date" name="expiredDateStr" id= "expiredDateStr" value="${position.expiredDateStr}">
                    </div>
                </div>
                <div class="field">
                    <label>备注</label>
                    <div class="ui input">
                        <input type="text" name="comment" id="comment" value="${position.comment}" placeholder="请输入备注">
                    </div>
                </div>
                <div class="field">
                    <label>选择要发布的平台</label>
                    <div class="ui center aligned segment" id="choice-buttons"> 
                        <div class="two ui buttons"> 
                        <c:choose>
                            <c:when test="${position.platformID == 1 }">
                                <div class="ui button dog" id="hunter">猎头平台</div> 
                                <div class="ui button active dog" id="recom">推荐平台</div> 
                            </c:when>
                            <c:when test="${position.platformID == 2 }">
                                <div class="ui button active dog" id="hunter">猎头平台</div> 
                                <div class="ui button dog" id="recom">推荐平台</div> 
                            </c:when>
                            <c:when test="${position.platformID == 3 }">
                                <div class="ui button active dog" id="hunter">猎头平台</div> 
                                <div class="ui button active dog" id="recom">推荐平台</div> 
                            </c:when>
                            <c:otherwise>
                                <div class="ui button dog" id="hunter">猎头平台</div> 
                                <div class="ui button dog" id="recom">推荐平台</div> 
                            </c:otherwise>
                          </c:choose>
                        </div>
                    </div>
                </div>
                <input type="hidden" name="positionid" id="positionid" value="${position.id}"/>
                <input type="hidden" name="domainid" id="domainid" value="${position.domainID}"/>
            </form> 
            &nbsp;<br>       
        </div>

        <div class="ui segment" id="cont-next" style="border:none;">
            <div class="fluid ui button" id="next">发布职位</div>
        </div>

</div>

<script src="${ctx}/resources/jweixin-1.0.0.js"></script>

<script type="text/javascript">

var dataarray=[];
dataarray[0] = "${position.majorDomainId1}";
dataarray[1] = "${position.majorDomainId2}";
dataarray[2] = "${position.majorDomainId3}";

wx.config({
    debug: false, 
    appId: "${config.appid}",  
    timestamp: ${config.currenttimestamp},  
    nonceStr: "${config.nonceStr}", 
    signature: "${config.signatureStr}", 
    jsApiList: ['checkJsApi',
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'hideOptionMenu']  
});

    var handler = { 
        activate: function() { 
            //$(this).addClass('active').siblings().removeClass('active'); 
            if($(this).attr('id') == 'hunter') {
                if($(this).hasClass('active')) {
                    $(this).removeClass('active'); 
                } else {
                    $(this).addClass('active');
                }
            }
            if($(this).attr('id') == 'recom') {
                if($(this).hasClass('active')) {
                    $(this).removeClass('active'); 
                } else {
                    $(this).addClass('active');
                }
            }

            if($(this).attr('id') == 'next') {
                var positionid  = $("#positionid").val();
                //var roleid      = $("#roleid").val();
                var cityid      = $("#cityId").val();
                var location    = $("#location").val();
                var employeeNum = $("#employeeNum").val()
                var majorDuty   = $("#majorDuty").val();
                var reward      = $("#reward").val();
                var expectcompanyid  = $("#expectcompanyid").val();
                var expectcompanyid2 = $("#expectcompanyid2").val();
                var expectcompanyid3 = $("#expectcompanyid3").val();
                var expectJob     = $("#expectJob").val();
                var expiredDateStr  = $("#expiredDateStr").val();
                var comment         = $("#comment").val();
                var domainid         = $("#domainid").val();
                var majordomain  = $("#majordomain").val();
                
                var platformid  = 2;

                var jobname = $('#jobtitle').search('get value');
                
                alert(expectJob);
                /*
                var jobname2 = "${position.name}";

                if(roleid == "") {
                    //alert($("#companyId").dropdown('get search')[0]);
                    if(!$("#roleid").dropdown('get search')[0] || $("#roleid").dropdown('get search')[0]=="" ) {
                        if(jobname2!=$("#roleid").dropdown('get text')[0]) {
                            alert("您必须输入职位名称");
                            return false;
                        } 
                        jobname = jobname2;
                        roleid = 0;
                        
                    } else {
                        jobname = $("#roleid").dropdown('get search')[0];
                        roleid   = 0;
                    }
                }
                */

                if($('#hunter').hasClass('active') && $('#recom').hasClass('active') ) {
                    platformid = 3;
                } else if($('#hunter').hasClass('active')) {
                    platformid = 2;
                } else if($('#recom').hasClass('active')) {
                    platformid = 1;
                } else {
                    platformid = 0;
                }

                if(!platformid) {
                    alert("您必须选择发布平台");
                    return false;
                }

                if(!location || location=="") {
                    alert("您必须输入负责区域");
                    return false;
                }
                if(location.length > 40) {
                    alert("负责区域名字太长");
                    return false;
                }
                if(!employeeNum || employeeNum =="") {
                    alert("您必须输入下属人数");
                    return false;
                }
                if(!/^[0-9]+$/.test(employeeNum) || employeeNum < 0 || employeeNum > 50000) {
                    alert("您必须输入合格的下属人数"); 
                    return false;
                }
                if(!majorDuty || majorDuty =="") {
                    alert("您必须主要职责");
                    return false;
                }
                if(majorDuty.length > 255) {
                    alert("主要职责名字太长");
                    return false;
                }
                /* if(!reward || reward =="") {
                    alert("您必须输入推荐奖金");
                    return false;
                } */
                if(!/^[0-9]+$/.test(reward) || reward < 0 || reward > 50000) {
                    alert("您必须输入合格的推荐奖金"); 
                    return false;
                }
                if(!jobname || jobname == "") {
                    alert("您必须输入职位名称");
                    return false;
                }
                if(jobname.length > 40) {
                    alert("输入职位名称太长");
                    return false;
                }
               
                if(!cityid) {
                    alert("您必须输入城市");
                    return false;
                }
                if(!expectcompanyid) {
                    alert("您必须输入期望的来源公司");
                    return false;
                }
                if(!expectcompanyid2) {
                    alert("您必须输入期望的来源公司");
                    return false;
                }
                if(!expectcompanyid3) {
                    alert("您必须输入期望的来源公司");
                    return false;
                }
                $(this).addClass('disabled');
                var that = this;

                var param = {};
                var postUrl = '${ctx}/wechat/hunter/creatJob.do';
                if (positionid && positionid == "0") {
                     postUrl = '${ctx}/wechat/hunter/creatJob.do';
                }
                else
                {
                    postUrl = '${ctx}/wechat/hunter/updateJob.do';
                    param['id']  = positionid;
                }
              
                param['name'] = jobname;
                //param['roleid']   = roleid;
                param['cityId']   = cityid;
                param['location'] = location;
                param['employeeNum'] = employeeNum;
                param['majorDuty']   = majorDuty;
                param['reward'] = reward;
                param['expectcompanyid']  = expectcompanyid;
                param['expectcompanyid2'] = expectcompanyid2;
                param['expectcompanyid3'] = expectcompanyid3;
                param['expectJobId']      = expectJob;
                param['expiredDateStr']   = expiredDateStr;
                param['platformID']       = platformid;
                param['comment']          = comment;
                param['domainID']         = domainid;

                var arrayLength = majordomain.length;
                if (arrayLength>0){
                  param['majorDomainId1'] = majordomain[0];
                  }
                if (arrayLength>1)
                {
                param['majorDomainId2'] = majordomain[1];
                }if (arrayLength>2)
                  {
                  param['majorDomainId3'] = majordomain[2];
                  }
		  
                $.ajax({ 
                    url: postUrl, 
                    type: "POST", 
                    data: param,
                    success: function(data, status){
                        if (data.errorcode == 0){
                            var job = data.data;
                            var forwardUrl = '${ctx}/wechat/hunter/toViewJob.page?positionid=' + job.id;
                            window.location.href = forwardUrl;
                        }else{
                            alert(data.msg);
                            $(that).removeClass('disabled');
                        }
                    }, 
                    error: function(){ 
                        alert("服务出错，请稍后尝试"); 
                        $(that).removeClass('disabled');
                    } 
                });
            }
        } 
    };

    var jobtitledata = [];
    
    <c:forEach items="${roles}" var="role">
        var tmp${role.id} = {};
        tmp${role.id}.title = "${role.name}";
        jobtitledata.push(tmp${role.id});
    </c:forEach>

    $(document).ready(function() { 
      $('#majordomain').dropdown('set value', dataarray);
      
     
      //allowAdditions: true,
      $('.dropdown').dropdown({
        maxSelections: 3,
        fullTextSearch:true
    });
    $('.ui.search').search({
        source: jobtitledata,
        searchFullText: true
    });
      
            $('.button').on('click', handler.activate);
           
            wx.ready(function() {
            wx.hideOptionMenu();
        });
    });

</script>
</body>
</html>